﻿var pageindex = getParam("pageindex");
var websiteId = getParam("websiteId");
var websiteData = "";
var bigclassData = "";

$(function () {    
    loadWebsites();
    loadBigclass();
    loadTitle();
    loadTables(pageindex, websiteId);
});

function loadTitle() {
    var tr = "<h3 class='box-title'><b>" + findWebsiteNameByWebsiteId(websiteId) +"</b>  原始信息</h3>";
    $("#title").html(tr);
}

//加载列表数据
function loadTables(pageindex, websiteId) {
    $("#tableBody").html("");
    
    $.ajax({
        type: "GET",
        url: "/Webmonitor/InfoOriginal/GetWebsiteJKNews1?pageindex=" + pageindex + "&websiteId=" + websiteId +"&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data.rows, function (i, item) {
                var tr = "<tr>";                
                //tr += "<td>" + findWebsiteNameByWebsiteId(item.websiteId) + "</td>";
                tr += "<td>" + findBigclassNameByWebsiteJKId(item.bigclassId) + "</td>";
                tr += "<td><a href=" + item.url + " target='_blank'>" + (item.title == null ? "" : item.title) + "</a></td>";
                tr += "<td>" + item.summary + "</td>";
                tr += "<td>" + item.createdOn.substr(5, 11).replace("T", " ") + "</td>";                
                tr += "</tr>";
                $("#tableBody").append(tr);
            })
            var elment = $("#grid_paging_part"); //分页插件的容器id
            if (data.rowCount > 0) {
                var options = { //分页插件配置项
                    bootstrapMajorVersion: 3,
                    currentPage: pageindex, //当前页
                    numberOfPages: data.rowsCount, //总数
                    totalPages: data.pageCount, //总页数
                    onPageChanged: function (event, oldPage, newPage) { //页面切换事件
                        loadTables(newPage, websiteId);
                    }
                }
                elment.bootstrapPaginator(options); //分页插件初始化
            }            
        }
    })
}

function loadWebsites() {    
    var option = "";
    $.ajax({
        type: "GET",
        url: "/Webmonitor/Website/GetAllListToJson?_t=" + new Date().getTime(),
        async: false,       //Ajax请求默认的都是异步的,此处设为同步
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            websiteData = data;            
        }
    });
}

function findWebsiteNameByWebsiteId(websiteId) {
    var html = "";
    $.each(websiteData, function (i, item) {
        if (item.id == websiteId) {
            html = item.websiteName;
            //alert(html);
            return html;
        }
    });
    return html;
}

function loadBigclass() {  
    var option = "";
    $.ajax({
        type: "GET",
        url: "/Webmonitor/WebsiteJK/GetAllListToJson?_t=" + new Date().getTime(),
        async: false,       //Ajax请求默认的都是异步的,此处设为同步
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            bigclassData = data;
        }
    });
}

function findBigclassNameByWebsiteJKId(websiteJKId) {
    var html = "";
    $.each(bigclassData, function (i, item) {
        if (item.id == websiteJKId) {
            html = item.bigclassName;
            //alert(html);
            return html;
        }
    });
    return html;
}

//获取URL上的参数
function getParam(paramName) {
    paramValue = "";
    isFound = false;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
        i = 0;
        while (i < arrSource.length && !isFound) {
            if (arrSource[i].indexOf("=") > 0) {
                if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
                    paramValue = arrSource[i].split("=")[1];
                    isFound = true;
                }
            }
            i++;
        }
    }
    return paramValue;
}